package com.example.nick.bodyapplication.xml

import android.os.Bundle
import android.util.Log
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import com.example.nick.bodyapplication.R
import java.io.InputStream
import javax.xml.parsers.SAXParser
import javax.xml.parsers.SAXParserFactory

class XmlActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_xml)
    }

    fun onSaxXml(view: View) {
//获取文件资源建立输入流对象
        //获取文件资源建立输入流对象
        val inputStream: InputStream = assets.open("person1.xml")
        //①创建XML解析处理器
        //①创建XML解析处理器
        val ss = SaxHelper()
        //②得到SAX解析工厂
        //②得到SAX解析工厂
        val factory: SAXParserFactory = SAXParserFactory.newInstance()
        //③创建SAX解析器
        //③创建SAX解析器
        val parser: SAXParser = factory.newSAXParser()
        //④将xml解析处理器分配给解析器,对文档进行解析,将事件发送给处理器
        //④将xml解析处理器分配给解析器,对文档进行解析,将事件发送给处理器
        parser.parse(inputStream, ss)
        inputStream.close()
        ss.persons?.forEach {
            Log.i("xml", "sax xml: $it")
        }
    }
    fun onDomXml(view: View) {
        val persons = DomHelper.queryXml(this)
        persons?.forEach {
            Log.i("xml", "dom xml: $it")
        }
    }
    fun onPullXml(view: View) {
        val inputStream = assets.open("person3.xml")
//        val persons = PullHelper.getPersons(inputStream)
//        val inputStream: InputStream = assets.open("person3.xml")
        val persons = PullHelper.queryXml(this, inputStream)
        persons?.forEach {
            Log.i("xml", "pull xml: $it")
            println("println pull xml: $it")
        }
    }
}